﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.TrangThaiService;
namespace eTMS.Controllers{
public class TrangThaiControllerBase : CommonController{
	public TrangThaiControllerBase()
		: base()
	{	
		this.dao = new TrangThaiServiceClient();
	}
	~TrangThaiControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private TrangThaiEntity oTrangThai;	
	public TrangThaiEntity TrangThaiObject{
		get{
			return oTrangThai;
		}
		set{
			oTrangThai=value;
		}
	}
	protected TrangThaiServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSTrangThai(false);			
			return _Table;
		}
	}
	public void LoadAllDSTrangThai(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oTrangThai==null)return false;
		bool result=true;
		try{
			oTrangThai = dao.SaveRet(oTrangThai);
		}
		catch{
			result=false;
		}
		if(result){
			_ListTrangThai.Add(oTrangThai);
		}
		return result;
	}
	public bool Update(){
		if(oTrangThai==null)return false;
		return dao.Update(oTrangThai);
	}
	public bool Delete(){
		if(oTrangThai==null)return false;
		bool retvalue = dao.Delete(oTrangThai);
		if(retvalue){			
			_ListTrangThai.Remove(oTrangThai);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 TrangThai_ID){
		bool retvalue = dao.DeleteByID(TrangThai_ID);		
		if(retvalue){
			TrangThaiEntity oDeleted=FindTrangThai(TrangThai_ID);
			if(oDeleted!=null){
				_ListTrangThai.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oTrangThai = new TrangThaiEntity();
	}
	//Check if exists MaTrangThai
	public bool CheckExistsMaTrangThai(object value){	
		return dao.SelectOneByMaTrangThai(value.ToString()).MaTrangThai!=null;
    }    
    //Copy data from data row in grid to object
    public TrangThaiEntity MapDataRowToObject(DataRow row){
		TrangThaiEntity o = new TrangThaiEntity();
		 o.MaTrangThai=Convert.ToString(row["MaTrangThai"]);
		 o.TenTrangThai=Convert.ToString(row["TenTrangThai"]);
		 o.MoTaTrangThai=Convert.ToString(row["MoTaTrangThai"]);
		 if(!row.IsNull("TrangThai_ID"))
		    o.TrangThai_ID=(Int32)(row["TrangThai_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(TrangThaiEntity o,DataRow row){
		row["MaTrangThai"]=o.MaTrangThai;
		row["TenTrangThai"]=o.TenTrangThai;
		row["MoTaTrangThai"]=o.MoTaTrangThai;
		row["TrangThai_ID"]=o.TrangThai_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.TrangThaiObject,row);
    }
	public void LoadObject(Int32 TrangThai_ID){
		TrangThaiEntity oLoad=FindTrangThai(TrangThai_ID);
		if(oLoad==null){
			oTrangThai=this.LoadTrangThai(TrangThai_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oTrangThai.IsNullTrangThai_ID;
			if(!bNotIsNull){
				this.ListTrangThai.Add(oTrangThai);
			}
		}
		else{
			oTrangThai=oLoad;
		}
	}
	#region CachedList TrangThai
	private static List<TrangThaiEntity> _ListTrangThai=new List<TrangThaiEntity>();
    public List<TrangThaiEntity>ListTrangThai{
		get{
			return _ListTrangThai;
		}
	}
	public static List<TrangThaiEntity>ListTrangThaiStatic{
		get{
			return _ListTrangThai;
		}
	}
	public TrangThaiEntity FindTrangThai(Int32 TrangThai_ID){
		TrangThaiEntity oResult=this.ListTrangThai.Find(delegate(TrangThaiEntity oFind){
			bool result=true; 
			result&=oFind.TrangThai_ID.Equals(TrangThai_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllTrangThai(bool clearCache){
		if(_ListTrangThai.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		TrangThaiServiceClient dao = new TrangThaiServiceClient();
		_ListTrangThai=new List<TrangThaiEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public TrangThaiEntity LoadTrangThai(Int32 TrangThai_ID){			
		TrangThaiEntity o=dao.SelectOne(TrangThai_ID);		
		return o;
	}
	#endregion
}
}
